﻿<%@ Page Language="C#" MasterPageFile="~/MasterPages/Core.Master" AutoEventWireup="true"
    CodeBehind="ProjectDetails.aspx.cs" Inherits="Pap.Web.Modules.Administration.Views.ProjectDetails"
    meta:resourcekey="PageResource1" %>

<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <script type="text/javascript">

        Ext.apply(Ext.form.VTypes,
            {
                'ToFromCompare': function(val, field) {
                    var fromValue = Ext.getCmp('txtFrom').getValue();
                    return (val > fromValue);
                }
            }
        );

        Ext.apply(Ext.form.VTypes,
            {
                'wndToFromCompare': function(val, field) {
                    var fromValue = Ext.getCmp('wndtxtFrom').getValue();
                    return (val > fromValue);
                }
            }
        );

        function ValidateStage() {
            var valid = Ext.getCmp('txtAgeLimit').isValid();
            valid &= Ext.getCmp('txtFrom').isValid();
            valid &= Ext.getCmp('txtTo').isValid();
            if (valid == 0)
                return false;
            return true;
        }

        function wndValidateForm() {
            return Ext.getCmp('wndMainForm').getForm().isValid();
        }

        function prepare(grid, toolbar, rowIndex, record) {
            var recordStage = record.data.Stage;
            var lastStage = Ext.getCmp('txtLastStage').getValue();

            var firstButton = toolbar.items.get(0);

            if (recordStage != lastStage) {
                firstButton.setDisabled(true);
            }
        }

        function CheckProjectNameExist(v, e) {
            Pap.ValidateName();
        }

        function ValidateForm() {
            var store = Ext.getCmp('gvStages').getStore();
            var count = store.getCount();
            if (count < 1) {
                Ext.getCmp('txtProjectName').markInvalid('<asp:Literal runat="server" Text="<%$ Resources:MBNoStagesAdded %>" />');
                return false;
            }
            else {
                return Ext.getCmp('MainForm').getForm().isValid();
            }
        }

        function gvStages_Command(command, recordId) {
            if (command == "Delete") {
                Ext.Msg.confirm('<asp:Literal runat="server" Text="<%$ Resources:MBConfirm %>" />', '<asp:Literal runat="server" Text="<%$ Resources:MBSureDelete %>" />', Pap.DeleteStage);
            }
        }
    </script>

    <ext:FitLayout runat="server" IDMode="Static">
        <ext:FormPanel ID="MainForm" Title="Add Project" runat="server" IDMode="Static" Frame="true"
            Border="true" BodyStyle="padding:10px 10px 0;" meta:resourcekey="MainFormResource1">
            <Defaults>
                <ext:Parameter Name="SelectOnFocus" Value="true" Mode="Raw" />
            </Defaults>
            <Body>
                <ext:Store ID="StagesStore" runat="server" IDMode="Static">
                    <Reader>
                        <ext:ArrayReader>
                            <Fields>
                                <ext:RecordField Name="Stage" Type="Int" />
                                <ext:RecordField Name="AgeLimit" Type="Int" />
                                <ext:RecordField Name="From" Type="Float" />
                                <ext:RecordField Name="To" Type="Float" />
                                <ext:RecordField Name="ProjectStageID" Type="String" />
                                <ext:RecordField Name="ProjectID" Type="String" />
                            </Fields>
                        </ext:ArrayReader>
                    </Reader>
                </ext:Store>
                <ext:Hidden ID="txtLastStage" runat="server" Text="0" IDMode="Static" />
                <ext:FormLayout runat="server">
                    <ext:Anchor Horizontal="70%">
                        <ext:TextField ID="txtProjectName" runat="server" FieldLabel="Project Name" MaxLength="50"
                            AllowBlank="False" meta:resourcekey="txtProjectNameResource1">
                        </ext:TextField>
                    </ext:Anchor>
                    <ext:Anchor Horizontal="70%">
                        <ext:NumberField ID="txtProfitPercent" runat="server" FieldLabel="Profit Percent"
                            MaxLength="8" MinValue="1" AllowBlank="False" meta:resourcekey="txtProfitPercentResource1">
                        </ext:NumberField>
                    </ext:Anchor>
                    <ext:Anchor Horizontal="70%">
                        <ext:TextField ID="txtParent" runat="server" FieldLabel="Parent" ReadOnly="True"
                            meta:resourcekey="txtParentResource1" Text="" />
                    </ext:Anchor>
                    <ext:Anchor Horizontal="70%" Vertical="100%">
                        <ext:Panel ID="pnlStages" runat="server" Frame="True" Title="Stages" meta:resourcekey="pnlStagesResource1">
                            <Body>
                                <ext:FitLayout runat="server">
                                    <ext:GridPanel ID="gvStages" StoreID="StagesStore" runat="server" TrackMouseOver="True"
                                        StripeRows="True" AutoWidth="True">
                                        <ColumnModel>
                                            <Columns>
                                                <ext:Column Header="<%$ Resources:HeaderStage %>" DataIndex="Stage" />
                                                <ext:Column Header="<%$ Resources:HeaderAgeLimit %>" DataIndex="AgeLimit" />
                                                <ext:Column Header="<%$ Resources:HeaderFrom %>" DataIndex="From" />
                                                <ext:Column Header="<%$ Resources:HeaderTo %>" DataIndex="To" />
                                                <ext:CommandColumn Width="110px" Fixed="True" Sortable="False">
                                                    <Commands>
                                                        <ext:GridCommand Icon="Delete" CommandName="Delete" IconCls="icon-delete">
                                                            <ToolTip Text="<%$ Resources:CommandDeleteToolTip %>" />
                                                        </ext:GridCommand>
                                                        <ext:GridCommand Icon="NoteEdit" CommandName="Edit" IconCls="icon-noteedit">
                                                            <ToolTip Text="<%$ Resources:CommandEditToolTip %>" />
                                                        </ext:GridCommand>
                                                    </Commands>
                                                    <PrepareToolbar Fn="prepare" />
                                                </ext:CommandColumn>
                                            </Columns>
                                        </ColumnModel>
                                        <View>
                                            <ext:GridView runat="server">
                                                <HeaderRows>
                                                    <ext:HeaderRow>
                                                        <Columns>
                                                            <ext:HeaderColumn>
                                                                <Component>
                                                                    <ext:TextField ID="txtStageNumber" runat="server" AllowBlank="False" ReadOnly="True"
                                                                        meta:resourcekey="txtStageNumberResource1" />
                                                                </Component>
                                                            </ext:HeaderColumn>
                                                            <ext:HeaderColumn>
                                                                <Component>
                                                                    <ext:NumberField ID="txtAgeLimit" runat="server" AllowBlank="False" MinValue="1"
                                                                        meta:resourcekey="txtAgeLimitResource1" IDMode="Static" />
                                                                </Component>
                                                            </ext:HeaderColumn>
                                                            <ext:HeaderColumn>
                                                                <Component>
                                                                    <ext:NumberField ID="txtFrom" runat="server" IDMode="Static" AllowBlank="False" meta:resourcekey="txtFromResource1" />
                                                                </Component>
                                                            </ext:HeaderColumn>
                                                            <ext:HeaderColumn>
                                                                <Component>
                                                                    <ext:NumberField ID="txtTo" runat="server" AllowBlank="False" Vtype="ToFromCompare"
                                                                        meta:resourcekey="txtToResource1" IDMode="Static" />
                                                                </Component>
                                                            </ext:HeaderColumn>
                                                            <ext:HeaderColumn AutoWidthElement="False">
                                                                <Component>
                                                                    <ext:LinkButton ID="btnAdd" runat="server" Text="Add" meta:resourcekey="btnAddResource1">
                                                                        <AjaxEvents>
                                                                            <Click OnEvent="btnAdd_Clicked" ViewStateMode="Include" Before=" return ValidateStage();" />
                                                                        </AjaxEvents>
                                                                    </ext:LinkButton>
                                                                </Component>
                                                            </ext:HeaderColumn>
                                                        </Columns>
                                                    </ext:HeaderRow>
                                                </HeaderRows>
                                            </ext:GridView>
                                        </View>
                                        <SelectionModel>
                                            <ext:RowSelectionModel runat="server" SingleSelect="True" />
                                        </SelectionModel>
                                        <AjaxEvents>
                                            <Command OnEvent="gvStages_Command" ViewStateMode="Include">
                                                <ExtraParams>
                                                    <ext:Parameter Name="CommandName" Value="command" Mode="Raw" Encode="False" />
                                                    <ext:Parameter Name="Stage" Value="record.data.Stage" Mode="Raw" Encode="False" />
                                                </ExtraParams>
                                            </Command>
                                        </AjaxEvents>
                                        <Listeners>
                                            <Command Handler="gvStages_Command(command, record.id);" />
                                        </Listeners>
                                    </ext:GridPanel>
                                </ext:FitLayout>
                            </Body>
                        </ext:Panel>
                    </ext:Anchor>
                </ext:FormLayout>
                <asp:CustomValidator runat="server" ControlToValidate="txtProjectName" ClientValidationFunction="CheckProjectNameExist"
                    OnServerValidate="txtProjectName_Validate" />
                <asp:CustomValidator runat="server" OnServerValidate="ValidateStages" />
            </Body>
            <BottomBar>
                <ext:StatusBar ID="FormStatusBar" runat="server" DefaultText="Ready">
                    <Plugins>
                        <ext:ValidationStatus ID="pnlValidation" FormPanelID="MainForm" ValidIcon="Accept"
                            ErrorIcon="Exclamation" HideText="<%$Resources:wndpnlValidationHideText %>" ShowText="<%$Resources:wndpnlValidationShowText %>" />
                    </Plugins>
                    <Items>
                        <ext:Button ID="btnSubmit" Text="Submit" runat="server" Icon="Disk" meta:resourcekey="btnSubmitResource1">
                            <AjaxEvents>
                                <Click OnEvent="btnSubmit_Clicked" Before="return ValidateForm();" />
                            </AjaxEvents>
                        </ext:Button>
                        <ext:Button ID="btnCancel" Text="Cancel" runat="server" Icon="Cancel" meta:resourcekey="btnCancelResource1">
                            <AjaxEvents>
                                <Click OnEvent="btnCancel_Clicked" />
                            </AjaxEvents>
                        </ext:Button>
                    </Items>
                </ext:StatusBar>
            </BottomBar>
        </ext:FormPanel>
    </ext:FitLayout>
    <ext:Window ID="wndEditStage" runat="server" Modal="True" Collapsed="True" ShowOnLoad="False"
        Width="300px" Resizable="False" Closable="False" Constrain="True" AutoHeight="True"
        IDMode="Static" CloseAction="Hide" Height="100px" meta:resourcekey="wndEditStageResource1"
        Title="Edit stage">
        <Body>
            <ext:FormPanel ID="wndMainForm" runat="server">
                <Body>
                    <ext:FormLayout IDMode="Static" runat="server" meta:resourcekey="FormLayoutResource1">
                        <ext:Anchor Vertical="90%" Horizontal="100%">
                            <ext:TextField ID="wndtxtStage" runat="server" FieldLabel="Stage" ReadOnly="True"
                                meta:resourcekey="wndtxtStageResource1" IDMode="Static" />
                        </ext:Anchor>
                        <ext:Anchor Vertical="90%" Horizontal="100%">
                            <ext:NumberField ID="wndtxtAgeLimit" runat="server" FieldLabel="Age Limit" MinValue="1"
                                AllowBlank="False" meta:resourcekey="wndtxtAgeLimitResource1" IDMode="Static" />
                        </ext:Anchor>
                        <ext:Anchor Vertical="90%" Horizontal="100%">
                            <ext:NumberField ID="wndtxtFrom" runat="server" AllowBlank="False" FieldLabel="From"
                                meta:resourcekey="wndtxtFromResource1" IDMode="Static" />
                        </ext:Anchor>
                        <ext:Anchor Vertical="90%" Horizontal="100%">
                            <ext:NumberField ID="wndtxtTo" runat="server" FieldLabel="To" AllowBlank="False"
                                Vtype="wndToFromCompare" meta:resourcekey="wndtxtToResource1" />
                        </ext:Anchor>
                    </ext:FormLayout>
                </Body>
                <BottomBar>
                    <ext:StatusBar ID="wndFormStatusBar" runat="server" DefaultText="Ready" meta:resourcekey="wndFormStatusBarResource1">
                        <Plugins>
                            <ext:ValidationStatus ID="wndpnlValidation" FormPanelID="wndMainForm" ValidIcon="Accept"
                                ErrorIcon="Exclamation" runat="server" ShowText="<%$Resources:wndpnlValidationShowText %>"
                                HideText="<%$Resources:wndpnlValidationHideText %>" />
                        </Plugins>
                        <Items>
                            <quadz:CoolitePermitButton ID="wndbtnSubmit" Text="Submit" runat="server" Icon="Disk"
                                meta:resourcekey="wndbtnSubmitResource1" OnClientClick="" PermissionName="SubmitProjectDetails"
                                HideAsDisabled="true">
                                <AjaxEvents>
                                    <Click OnEvent="wndbtnSubmit_Clicked" ViewStateMode="Include" Before="return wndValidateForm();" />
                                </AjaxEvents>
                            </quadz:CoolitePermitButton>
                            <ext:Button ID="wndbtnCancel" Text="Cancel" runat="server" Icon="Cancel" meta:resourcekey="wndbtnCancelResource1">
                                <AjaxEvents>
                                    <Click OnEvent="wndbtnCancel_Clicked" ViewStateMode="Include" />
                                </AjaxEvents>
                            </ext:Button>
                        </Items>
                    </ext:StatusBar>
                </BottomBar>
            </ext:FormPanel>
        </Body>
    </ext:Window>
</asp:Content>
